<?php
/**
 * common model file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/09/04
 * Time: 17:52
 */

namespace HiCommon\Model;

class UsersVehicle extends Base
{

    protected $table = 'users_vehicle';

    protected $guarded = [];

    //public $timestamps = false;

    /**
     * 应该被调整为日期的属性
     *
     * @var array
     */
    protected $dates
      = [
      ];


    /**
     * 关联最近一次检测
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function nearly_check(){
        return $this->hasOne(OrderCheckpointVehicle::class,'users_vehicle_id')->whereIn('check_state',[2,3])->orderByDesc('checked_at')->select(['id','users_vehicle_id','checked_at']);
//        return $this->hasOne(OrderCheckpointVehicle::class,'users_vehicle_id')->whereIn('check_state',[2,3])->order('created_at','desc')->select(['id','users_vehicle_id','checked_at']);
    }

    /**
     * 关联车型表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function vehicle_model(){
//        ->select(['id','name','parentid'])
        return $this->belongsTo(VehicleModel::class,'vehicle_model_id');
    }

//    /**
//     * 关联车型表
//     *
//     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
//     * @author wumengmeng <wu_mengmeng@foxmail.com>
//     */
//    public function simple_vehicle_model(){
//        //        ->select(['id','name','parentid'])
//        return $this->belongsTo(ViewSimpleVehicleModel::class,'vehicle_model_id')->select(['id','name','one_name','two_name','three_name','logo']);
//    }

    /**
     * 关联车型表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function simple_vehicle_model(){
        //        ->select(['id','name','parentid'])
        return $this->belongsTo(ViewSimpleVehicleModel::class,'vehicle_model_id')->select(['id','name','one_name','two_name','three_name','logo']);
    }

    /**
     * 关联车型表
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function whole_vehicle_model(){
        //        ->select(['id','name','parentid'])
        return $this->belongsTo(VehicleModel::class,'vehicle_model_id')->with('parent.parent.parent')->select(['id','name','parentid']);
    }

    /**
     * 关联用户车辆保险表
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function users_vehicle_insurance(){
        //        ->select(['id','name','parentid'])
        return $this->hasOne(UsersVehicleInsurance::class,'users_vehicle_id');
    }

    /**
     * 获取状态对应的名称
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getStateAttribute($value)
    {
        switch ($value) {
            case 0:
                $res = ['id' => 0, 'name' => '草稿'];
                break;
            case 1:
                $res = ['id' => 1, 'name' => '待审核'];
                break;
            case 2:
                $res = ['id' => 2, 'name' => '通过'];
                break;
            case 3:
                $res = ['id' => 3, 'name' => '拒绝'];
                break;
            default:
                $res = ['id' => -1, 'name' => '未知'];
        }
        return $res;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getCarImgAttribute($value)
    {
        $arr_data = [
          'src'      => $value,
          'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    /**
     * 获取图片可用链接
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getDrivingPermitImgAttribute($value)
    {
        $arr_data = [
          'src'      => $value,
          'full_src' => oss_full_url($value),
        ];
        return $arr_data;
    }

    /**
     * 扩展信息
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function getExtendAttribute()
    {
        $arr_data        = [
          'created_at' => ymd_friendly_date($this->created_at),
        ];
        $arr_data['api'] = $this->table_field_api();

        return $arr_data;
    }


}